package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.RolePermissionMapper;
import com.gxa.insurance.pojo.RolePermission;
import com.gxa.insurance.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wang li sha
 * @date 2022-06-06 16:33
 */
@Service("RolePermissionService")
@Transactional(rollbackFor = Throwable.class)
public class RolePermissionServiceImpl implements RolePermissionService {
    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public Integer insert(List<RolePermission> rolePermissions) throws Exception {
        Integer count = 0;
        for (RolePermission rolePermission : rolePermissions) {
            Integer insert = rolePermissionMapper.insert(rolePermission);
            if (insert != 1) {
                throw new BusinessException(ExceptionCode.INSERT_ROLEPERMISSION_FAILED);
            }
            count++;
        }
        return count;
    }

    @Override
    public Integer deleteByRoleId(Integer roleId) throws Exception {
        Integer delete = rolePermissionMapper.deleteByRoleId(roleId);
        if (delete < 1) {
            throw new BusinessException(ExceptionCode.DELETE_ROLEPERMISSION_FAILED);
        }
        return delete;
    }
}
